package cn.darkhorse.mesadmin.controller.ticket;


import cn.darkhorse.ticket.basis.pojo.TicketProcedure;
import cn.darkhorse.ticket.basis.service.TicketProcedureService;
import cn.darkhorse.ticket.code.RepeatCheck;
import cn.darkhorse.ticket.constants.TicketConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 工序 前端控制器
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/mesticket/basisProcedure")
public class TicketProcedureController extends BaseController{
	
	@Resource
	private TicketProcedureService procedureService;
	@Resource
	private RepeatCheck<TicketProcedure> repeatCheck;
	/**
	 * 列表
	 * @param code
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public TableDataInfo list(String code, String name){
		startPage();
		LambdaQueryWrapper<TicketProcedure> queryWrapper = new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(code)){
			queryWrapper.like(TicketProcedure::getCode,code);
		}
		if(StringUtils.isNotBlank(name)){
			queryWrapper.like(TicketProcedure::getName,name);
		}
		queryWrapper.orderByDesc(TicketProcedure::getGmtCreate);
		List<TicketProcedure> procedureList = procedureService.list(queryWrapper);
		return getDataTable(procedureList);
	}
	
	/**
	 * 添加
	 * @param procedure
	 * @return
	 */
	@RepeatSubmit
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public AjaxResult add(@RequestBody  @Validated TicketProcedure procedure){
		//校验编码、名称
		//String s = procedureService.repeat(procedure);
		String s = repeatCheck.repeat(procedureService, procedure, TicketConstants.BUSINESS_TYPE_GX);
		if(StringUtils.isNotBlank(s)){
			procedure.setCode(s);
		}
		procedureService.save(procedure);
		return AjaxResult.success();
	}
	
	/**
	 * 修改
	 * @param procedure
	 * @return
	 */
	@RepeatSubmit
	@RequestMapping(value = "/modify",method = RequestMethod.POST)
	public AjaxResult modify(@RequestBody @Validated TicketProcedure procedure){
		
		boolean quote = procedureService.isQuote(procedure.getId());
		if(!quote){
			return AjaxResult.error("该工序已在使用，无法操作！");
		}
		//校验名称
		//procedureService.repeat(procedure);
		repeatCheck.repeat(procedureService, procedure, TicketConstants.BUSINESS_TYPE_GX);
		procedureService.updateById(procedure);
		return AjaxResult.success();
	}
	
	/**
	 * 是否引用
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/is-quote",method = RequestMethod.GET)
	public AjaxResult isQuote(Long id){
		boolean quote = procedureService.isQuote(id);
		return AjaxResult.success(quote);
	}
	
	@RepeatSubmit(interval = 2000)
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public AjaxResult delete(Long id){
		
		boolean quote = procedureService.isQuote(id);
		if(!quote){
			return AjaxResult.error("该工序已在使用，无法操作！");
		}
		procedureService.removeById(id);
		return AjaxResult.success();
	}
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/view",method = RequestMethod.GET)
	public AjaxResult view(Long id){
		TicketProcedure procedure = procedureService.getById(id);
		return AjaxResult.success(procedure);
	}
	

}

